package oocl.eshop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oocl.eshop.dao.FoodDao;
import oocl.eshop.po.Food;
import oocl.eshop.util.dbutil.DbUtil;

public class FoodDaoImpl implements FoodDao {
	public int AddFood(Food f) {
		String sql = "insert into food (fId, fName, mId, fPrice, fViewPath)values (elema_sql02.nextval, ?,?,?,?)";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		int n = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, f.getfName());
			pst.setInt(2, f.getmId());
			pst.setDouble(3, f.getfPrice());
			pst.setString(4, f.getfViewPath());
			n = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("添加异常");
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}

		return n;
	}

	public int deleteFood(int fId) {
		String sql = "delete from food where fid = ?";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, fId);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			System.out.println("删除异常,无此id food");
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	public List<Food> findAllFood(int mId) {
		List<Food> fs = new ArrayList<Food>();

		String sql = "select * from food where mId=?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, mId);
			rs = pst.executeQuery();
			while (rs.next()) {
				int fId = rs.getInt("fId");
				String fName = rs.getString("fName");
				double fPrice = rs.getDouble("fPrice");
				String fViewPath = rs.getString("fViewPath");

				Food food = new Food(fName, mId, fPrice, fViewPath);
				food.setfId(fId);
				fs.add(food);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return fs;
	}

	public int updateFood(Food f) {
		String sql = "update food set fName=?,fPrice=?,fViewPath=? where fid=?";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		int n = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, f.getfName());
			pst.setDouble(2, f.getfPrice());
			pst.setString(3, f.getfViewPath());
			pst.setInt(4, f.getfId());
			n = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}

		return n;
	}

	public List<Food> findFoodByName(String fName) {
		List<Food> fs = new ArrayList<Food>();

		String sql = "select * from food where fName=? ";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, fName);
			rs = pst.executeQuery();
			while (rs.next()) {
				int fId = rs.getInt("fId");
				int mId = rs.getInt("mId");
				double fPrice = rs.getDouble("fPrice");
				String fViewPath = rs.getString("fViewPath");

				Food food = new Food(fName, mId, fPrice, fViewPath);
				food.setfId(fId);
				fs.add(food);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return fs;
	}

	public Food findFoodById(int fId) {
		Food food = null;

		String sql = "select * from food where fId=? ";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, fId);
			rs = pst.executeQuery();
			if (rs.next()) {
				int mId = rs.getInt("mId");
				String fName = rs.getString("fName");
				double fPrice = rs.getDouble("fPrice");
				String fViewPath = rs.getString("fViewPath");

				food = new Food(fName, mId, fPrice, fViewPath);
				food.setfId(fId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return food;
	}
}
